package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.BookDao;
import com.dao.BookDaoImpl;

import vo.Book;

/**
 * Servlet implementation class BookServlet
 */
@WebServlet("/save.do")
public class BookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		Book book=new Book();
		book.setBookname(request.getParameter("bookname"));
		Integer price=99;
		if(request.getParameter("bookname") != null && !"".equals(request.getParameter("bookname"))) {
		 price=Integer.valueOf(request.getParameter("bookprice"));
		}
		book.setPrice(price);
		BookDao dao=new BookDaoImpl();
		dao.save(book);
		System.out.println("添加完成");
		response.setContentType("text/html;charset=utf-8");
		response.sendRedirect("/javaee-test/bookList.do");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
